package com.dingreading.cloud.ssxcx.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.aliyun.MstCdnUtil;
import com.dingreading.cloud.ssxcx.service.XcxStudentHomeworkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/xcx/studentHomework")
@Api(tags = "小程序-学生课后作业")
public class XcxStudentHomeworkController {
    @Resource
    private XcxStudentHomeworkService xcxStudentHomeworkService;

    @ApiOperation(value = "获取该学生的课后作业信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            HttpServletRequest request,
            @ApiParam(value = "伙伴uid") @RequestParam(value = "agencyUid") String agencyUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生studentUid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "课节uid") @RequestParam(value = "subjectLessonUid") String subjectLessonUid,
            @ApiParam(value = "课后题uid") @RequestParam(value = "questionUid") String questionUid
    ) {
        R<Object> r = MstCdnUtil.getIp(request);
        if (R.isError(r))
            return r;
        String ip = (String) r.getData();

        return xcxStudentHomeworkService.getBean(agencyUid, storeUid, studentUid, subjectLessonUid, questionUid, ip);
    }

    @ApiOperation(value = "学生课后作业具体状态")
    @PostMapping(value = "/getBeanStatus", produces = "application/json; charset=UTF-8")
    public R<Object> getBeanStatus(
            @ApiParam(value = "伙伴uid") @RequestParam(value = "agencyUid") String agencyUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生studentUid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "课节uid") @RequestParam(value = "subjectLessonUid") String subjectLessonUid,
            @ApiParam(value = "课后题uid") @RequestParam(value = "questionUid") String questionUid
    ) {
        return xcxStudentHomeworkService.getBeanStatus(agencyUid, storeUid, studentUid, subjectLessonUid, questionUid);
    }

    @ApiOperation(value = "上传学生课后作业,未批改通过uid修改图片")
    @PostMapping(value = "/uploadHomework", produces = "application/json; charset=UTF-8")
    public R<Object> uploadHomework(
            @ApiParam(value = "伙伴uid") @RequestParam(value = "agencyUid") String agencyUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生studentUid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "课节uid") @RequestParam(value = "subjectLessonUid") String subjectLessonUid,
            @ApiParam(value = "课后题uid") @RequestParam(value = "questionUid") String questionUid,
            @ApiParam(value = "答题uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "作业图片urls集合") @RequestParam(value = "imageUrls") List<String> imageUrls
    ) {
        return xcxStudentHomeworkService.uploadHomework(agencyUid, storeUid, studentUid, subjectLessonUid, questionUid, uid, imageUrls);
    }

    @ApiOperation(value = "上传课后作业到阿里云")
    @PostMapping(value = "/getOssHomework", produces = "application/json; charset=UTF-8")
    public R<Object> getOssHomework(
            @ApiParam(value = "文件类型") @RequestParam(value = "fileType") String fileType
    ) {
        return xcxStudentHomeworkService.getOssHomework(fileType);
    }

    @ApiOperation(value = "获取课后作业图片查看地址")
    @PostMapping(value = "/getHomeworkUrl")
    public R<Object> getHomeworkUrl(
            HttpServletRequest request,
            @ApiParam(value = "图片地址") @RequestParam(value = "url") String url
    ) {
        R<Object> r = MstCdnUtil.getIp(request);
        if (R.isError(r))
            return r;
        String ip = (String) r.getData();

        return xcxStudentHomeworkService.getHomeworkUrl(url, ip);
    }

}
